#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <DynamicArray.h>
#include <MyString.h>
#include "Scene.h"
#include "player.h"
#include "monster.h"
#include "StdFile.h"
#define monsterdamage ((m->attack-p->defence>0)?m->attack-p->defence:0)
#define playerdamage ((p->attack-m->defence>0)?p->attack-m->defence:0)

typedef struct User
{
    char name[20];
    char passwd[20];
}User;

bool Battle(Player *p,Monster *m)
{
    if(m->HP==1)
    {
        m->HP=0;
        system("clear");
        int temp=(rand() % 10*5)+rand()%10;
        p->Gold += temp;
        printf("在路上遇到一个宝箱,爆了些金币出来。\n");
        printf("你数了数，一共有%d个。\n",temp);
        temp=0;
        sleep(2);
        return PlayerMove;
    }
    system("clear");
    printf("你遇到了[%s]",m->name.string);
    printf("敌方信息:名称:[%s],血量🩸:%d 攻击力🗡:%d\n",m->name.string,m->HP,m->attack);
    sleep(2);                       //让程序休眠
    while(1)
    {
        system("clear");            //清屏
        printf("你的剩余血量💗:%d 攻击力🗡:%d 防御力🛡:%d\n",p->HP,p->attack,p->defence);
        printf("[%s]的剩余血量🩸:%d 攻击力🗡:%d 防御力🛡:%d\n",m->name.string,m->HP,m->attack,m->defence);
        printf("请进行你的选择:\n");
        printf("1.🔪进攻\n");
        printf("2.💥蓄力一击\n");
        printf("3.💨跑路\n");
        
        int choice =0;
        while(getchar()!='\n');     //清除缓冲区内的换行符
        scanf(" %d",&choice);
        switch (choice)
        {
            
            case 1:
            {   
                printf("你对[%s]发起了攻击,造成了%d伤害\n",m->name.string,playerdamage);
                m->HP-=playerdamage;
                if(playerdamage==0)                       //玩家没能破防
                {
                    printf("没想到皮太厚了，没能破防😅\n");
                }
                if(m->HP>0)
                {
                    printf("[%s]对你发起了攻击,造成了%d伤害\n",m->name.string,monsterdamage);           
                    p->HP -= monsterdamage;
                    if(monsterdamage==0)                       //怪物没能破防
                    {
                        printf("我这金刚不坏之身岂是你能破防的，下辈子多练练吧🤣\n");
                    }
                }
                
            }
                break;

            case 2:
            {
                if(GetRand(3)==1)
                {
                    printf("你瞄准[%s]的弱点发起了蓄力一击,精准命中,触发了暴击,无视防御,造成了%d伤害,效果拔群!\n",m->name.string,p->attack*2);
                    m->HP-=p->attack*2;  
                    if(m->HP>0)
                    {
                        if(IsEqual(&m->name,"👿恶魔撒旦")==false)
                            printf("[%s]被打的头晕眼花💫,甚至忘记了反击。\n",m->name.string);
                        else
                            {
                                printf("BOSS暴怒了🤬💢,眼中的怒火仿佛就要将你吞噬。\n"); 
                                printf("BOSS攻击力得到了提升⤴️,防御力小幅降低⤵️。\n");
                                m->attack+=3;
                            if(m->defence>=1)
                                m->defence-=1;
                                printf("[%s]对你发起了攻击,造成了%d伤害\n",m->name.string,monsterdamage);           
                                p->HP -= monsterdamage;
                                if(monsterdamage==0)                       //怪物没能破防
                                {
                                    printf("我这金刚不坏之身岂是你能破防的，下辈子多练练吧🤣\n");
                                }
                            }
                    }
                    

                    break;
                }
                else
                {
                    printf("你瞄准[%s]的弱点发起了蓄力一击,可惜打偏了,暴击失败,但是仍然造成了%d伤害\n",m->name.string,playerdamage/2);
                    m->HP-=playerdamage/2;
                    if((playerdamage/2)<=0)                   
                        {printf("没想到皮太厚了，没能破防😅\n");}
                    if(m->HP>0)
                    {
                        printf("[%s]对你发起了攻击,造成了%d伤害\n",m->name.string,monsterdamage);           
                        p->HP -= monsterdamage;
                        if(monsterdamage==0)                   
                        {printf("我这金刚不坏之身岂是你能破防的，下辈子多练练吧🤣\n");}
                    }
                   
                }
            }
                break;
            case 3:
            {
                if(IsEqual(&m->name,"👿恶魔撒旦")==true)
                {
                    printf("你被BOSS困在结界里,周围四下无门,插翅难逃,只能死战。\n");
                    printf("[%s]对你发起了攻击,造成了%d伤害\n",m->name.string,monsterdamage);
                    p->HP-=monsterdamage;
                    if(monsterdamage==0)                 
                        {printf("好险好险，还好没事。💦\n");}
                    break;
                }
                if(GetRand(2)==1)
                {
                    printf("你灵机一动决定跑路🏃🏼💨,润的飞快,即使是09年的博尔特见了都自愧不如。\n");
                    return true;                        //存活
                }
                else
                {
                    printf("你逃跑时慌不择路，%s又追了上来。\n",m->name.string);
                    printf("[%s]对你发起了攻击,造成了%d伤害\n",m->name.string,monsterdamage);
                    p->HP-=monsterdamage;
                    if(monsterdamage==0)                 
                        {printf("好险好险，还好没事。💦\n");}
                }
            }
                break;
            
            default:
                break;
        }
        sleep(1);  
        if(p->HP<=0)
        {
            p->HP=0;
            system("clear");
            if(IsEqual(&m->name,"👿恶魔撒旦")==true)
            {
                sleep(1);
                printf("你成为了恶魔撒旦的祭品...\n");
                sleep(1);
            }
            else
            {
                sleep(1);
                printf("你死在了[%s]手上\n",m->name.string);
                sleep(1);
            }
            printf("真的是太逊了!菜就多练!\n");
            return false;
            sleep(1);
        }
        if(m->HP<=0)
        {
            m->HP=0;
            system("clear");
            printf("[%s]被击败了!\n",m->name.string);
            p->exp += m->exp;
            p->Gold += m->Gold;
            if(IsEqual(&m->name,"👿恶魔撒旦")==false)
            printf("[%s]掉落了%d点经验📗%d个金币💰\n",m->name.string,m->exp,m->Gold);
            return true;
            sleep(1);
        }
        sleep(1);  
    }
}
void LogIn();
bool Register();
void WelcomeInterface()
{
    system("clear");
    printf("========================欢迎游玩魔塔游戏======================\n");
    printf("╔════════════════════════════════════════════════════════════╗\n");
    printf("║                                                            ║\n");
    printf("║                         MAGIC TOWER                        ║\n");
    printf("║                                                            ║\n");
    printf("║           ┃ 登录账号 ┃               ┃ 注册账号 ┃          ║\n");
    printf("║                                                            ║\n");
    printf("║                                                            ║\n");
    printf("║                                                            ║\n");
    printf("║                                               ┃ 退出游戏 ┃ ║\n");
    printf("╚════════════════════════════════════════════════════════════╝\n");
    printf("按“1”登录账号\n");
    printf("按“2”注册账号\n");
    printf("按“3”退出游戏\n");
    int choice=0;
    scanf(" %d",&choice);
    printf("\n");
    switch(choice)
    {
        case 1:   
            {
                LogIn();
            }
        break;

        case 2:
            {
                Register();
            }
        break;

        default:
        exit(0);

}
}

bool Register()
{   
    int flag1=0;
    int flag2=0;
    system("clear");
    MyStrList content = GetlineFromFile("/home/lvguanzhong/桌面/suqian/1122/账号库.txt");
    FILE * f=fopen("/home/lvguanzhong/桌面/suqian/1122/账号库.txt","a");
    if(f == NULL)
    {
        printf("账号库拉取失败！\n");
        return false;
    }
    printf("请输入注册的用户名:");
    char name[20]={0};  
    scanf("%s",name);
    char *enter="\n";
    AppendToFile("账号库.txt",enter,strlen(enter));
    if(AppendToFile("账号库.txt",name,strlen(name))==true)
    flag1=1;
    char *space=" ";
    AppendToFile("账号库.txt",space,1);
    printf("\n");
    printf("请输入密码:");
    char passwd[10]={0};
    scanf("%s",passwd);
    if(AppendToFile("账号库.txt",passwd,strlen(passwd))==true)
    flag2=1;
    fclose(f);
    if(flag1==1&&flag2==1)
    {
        system("clear");
        printf("新账号已注册！请登录！\n");
        LogIn();
    }
    else
    {
        printf("注册出错了!请重试!\n");
        return false;
    }    
    return true;
}
char name[20];
void LogIn()
{
    system("clear");
    
    char passwd[10];
    printf("请输入用户名和密码:\n");
    printf("用户名:");
    scanf("%s",name);
    printf("\n密码:");
    scanf("%s",passwd);

    MyStrList content = GetlineFromFile("/home/lvguanzhong/桌面/suqian/1122/账号库.txt");
    for(int i=0;i<content.len;i++)
    {
        User temp;
        sscanf(content.stringlist[i].string,"%s %s",temp.name,temp.passwd);
        if(strcmp(temp.name,name)==0 && strcmp(temp.passwd,passwd)==0)
        {
            printf("登录成功!\n");
            sleep(2);
            return ;
        }    
    }
        printf("登录失败!\n");
        printf("用户名与密码不匹配，请重新输入!\n");
        sleep(1.5);
        LogIn();

}

void SaveInterface()
{
    system("clear");
    printf("=========================请选择你的存档=======================\n");
    printf("╔════════════════════════════════════════════════════════════╗\n");
    printf("║                                                            ║\n");
    printf("║                                                            ║\n");
    printf("║             ┃ 存档一 ┃                ┃ 存档二 ┃           ║\n");
    printf("║             ┃ 存档三 ┃                ┃ 存档四 ┃           ║\n");
    printf("║                                                            ║\n");
    printf("║                                                  ┃ 退出 ┃  ║\n");
    printf("╚════════════════════════════════════════════════════════════╝\n");    
}


void Save(Player *p,DArray *ms)
{
    MyStr PTS;
    PTS=PlayerToString(p); 
    MyStr MTS;
    MTS=MonsterToString(ms); 

    system("clear");
    SaveInterface();
    printf("请选择你的存档:\n");
    printf("按“1”保存为存档一\n");
    printf("按“2”保存为存档二\n");
    printf("按“3”保存为存档三\n");
    printf("按“4”保存为存档四\n");
    printf("按“5”退出\n");
    int choice=0;
    scanf(" %d",&choice);
    printf("\n");
    switch(choice)
    {
        case 1:
        {
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档一p",PTS.string,PTS.len);
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档一m",MTS.string,MTS.len);
            printf("保存成功！\n");
            sleep(2);
            system("clear"); 
        }
        break;
        case 2:
        {
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档二p",PTS.string,PTS.len);
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档二m",MTS.string,MTS.len);
            printf("保存成功！\n");
            sleep(2);
            system("clear"); 
        }
        break;
        case 3:
        {
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档三p",PTS.string,PTS.len);
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档三m",MTS.string,MTS.len);
            printf("保存成功！\n");
            sleep(2);
            system("clear"); 
        }
        break;
        case 4:
        {
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档四p",PTS.string,PTS.len);
            WriteToFile("/home/lvguanzhong/桌面/suqian/1122/存档库/存档四m",MTS.string,MTS.len);
            printf("保存成功！\n");
            sleep(2);
            system("clear"); 
        }
        break;
        default:
        break;
    }
}



int main()
{  
    WelcomeInterface();
    
    //初始化场景
    char* map[MAPSIZE][MAPSIZE]={0};
    InitMap(map);
    
    //初始化玩家
    Player p;
    InitPlayer(&p);
    map[p.x][p.y]=p.symbol;
    
    //初始化怪物
    INITDARRAY(monsters);
    SetMonsters(&monsters);
    SetMonstersPosition(&monsters,map);

    PrintMap(map);
    Monster *posM = NULL; 
    while(1)
    {
        PrintPlayer(&p);
        PlayerMove(&p,map,&monsters);

        if(posM != NULL)
        {
            map[posM->x][posM->y] = posM->symbol;
            posM = NULL;
        }

        Monster *m = GetMonsterByPosition(&monsters,p.x,p.y);
        if(m != NULL)
        {
            if(Battle(&p,m) == false)
            {
                printf("你阵亡了!游戏结束!\n");
                return -1;
            }
            LevelUp(&p);
            
            if(m->HP == 0)
            {
                if(IsEqual(&m->name,"👿恶魔撒旦")==true)
                {
                    printf("你击败了恶魔撒旦!世界重归和平,恢复了宁静美好的样子。\n");
                    return 0;
                }
                map[m->x][m->y] = p.symbol;
            }
            else
            {
                posM = m;
            }
        }
        PrintMap(map);
        MyStr PTS;

    }

    return 0;
}

